package com.TetrisII.views;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import com.TetrisII.control.ShapeControllers;
import com.TetrisII.entities.Ground;
import com.TetrisII.entities.ShapeFactory;
import com.TetrisII.voice.ShapeVoice;

/**
 * 主窗体
 * 
 * @author Administrator
 *
 */
public class MainFrame extends JFrame implements ActionListener, FocusListener {
	private GamePanel gamePanel;// 游戏面板
	private LowPanel lowPanel;// 右边底层面板
	private MainMenu mainMenu;// 主菜单
	private PreviewPanel previewPanel;// 预览面板
	private GoalsPanel goalsPanel;// 得分面板
	private ControlPanel controlPanel;// 控制面板
	private ShapeControllers control;// 控制器
	private Ground ground;// 障碍物
	private ShapeFactory shapeFactory;// 工厂
	private UserDialog userDialog;// 自定义对话框

	public MainFrame(String title) {
		super(title);
		setFocusable(true);
		// 背景音乐
		new ShapeVoice().backGroundVoice();
		gamePanel = new GamePanel();
		ground = new Ground();
		shapeFactory = new ShapeFactory();
		control = new ShapeControllers(gamePanel, shapeFactory, ground);// 建立控制器
		// 面板注册监听
		gamePanel.addKeyListener(control);
		gamePanel.addFocusListener(this);
		this.addKeyListener(control);
		this.addFocusListener(this);
		previewPanel = new PreviewPanel(shapeFactory);// 工厂加入到预览面板中
		control.setPreview(previewPanel);// 预览面板加入控制器控制
		control.setMianFrame(this);
		goalsPanel = new GoalsPanel(ground);
		controlPanel = new ControlPanel();
		lowPanel = new LowPanel(previewPanel, goalsPanel, controlPanel);

		mainMenu = new MainMenu();
		// 菜单栏添加监听

		// 菜单项目监听
		mainMenu.getCustomMenu().addActionListener(this);
		mainMenu.getLevel_1().addActionListener(this);
		mainMenu.getLevel_2().addActionListener(this);
		mainMenu.getLevel_3().addActionListener(this);
		setJMenuBar(mainMenu);

		// 自定义对话框

		setSize(500, 600);
		setResizable(false);// 不可改变大小
		add(gamePanel, BorderLayout.WEST);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		add(lowPanel, BorderLayout.EAST);
		setLocationRelativeTo(null);// 屏幕中央
		control.beginGame();
		setVisible(true);
		pack();

		// 添加窗口事件监听
		addWindowListener(new WindowListener() {

			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub
			}

			/**
			 * 缩小时暂停
			 */
			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub
				control.setPause(true);
			}

			/**
			 * 打开时继续
			 */
			@Override
			public void windowDeiconified(WindowEvent e) {

				control.setPause(false);
			}

			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub
			}

			/**
			 * 退出确认
			 */
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				int n = JOptionPane.showConfirmDialog(null, "您是否要退出游戏？", "退出游戏", JOptionPane.YES_NO_OPTION);
				if (n == 0) {
					System.exit(0);
				}
			}

			@Override
			public void windowClosed(WindowEvent e) {

				// TODO Auto-generated method stub
			}

			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub
			}
		});

	}

	/**
	 * 菜单项监听事件，控制菜单栏中的三个等级选项事件
	 */
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == mainMenu.getLevel_1()) {
			control.setShapeSpeed(800);
			control.setShapeLevel(1);
			control.restartGame();
		}
		if (e.getSource() == mainMenu.getLevel_2()) {
			control.setShapeSpeed(400);
			control.setShapeLevel(2);
			control.restartGame();
		}
		if (e.getSource() == mainMenu.getLevel_3()) {
			// 有bug,速度太快时，有时候切换速度，方块还在下落不能初始化方块
			control.setShapeSpeed(200);
			control.setShapeLevel(3);
			control.restartGame();
		}
		if (e.getSource() == mainMenu.getCustomMenu()) {
			userDialog = new UserDialog(control);
		}
	}

	/**
	 * 焦点回来时重新开始
	 */
	@Override
	public void focusGained(FocusEvent e) {
		// TODO Auto-generated method stub
		if (control.isFrameContinue()) {
			control.continueGame();
		}
		control.setFrameContinue(false);
	}

	/**
	 * 失去焦点时暂停
	 */
	@Override
	public void focusLost(FocusEvent e) {
		// TODO Auto-generated method stub
		if (control.isFrameContinue() == false) {
			control.pauseGame();
		}
		control.setFrameContinue(true);
	}

}
